<template>
  <div id="sign" v-if="show">
    <div class="bg" @click="clear" v-if="show"></div>
    <div class="modal" v-if="isRechange == 0">
      <div class="modal-top">
        <div class="modal-title">充值加速币</div>
        <div class="modal-clear" @click="clear">
          <img src="../assets/imgs/shanchu.png" alt />
        </div>
      </div>
      <div class="modal-btns" v-if="isCode">
        <div
          class="modal-single"
          v-for="(item, index) in modal"
          :key="index"
          :class="item.show ? 'selected' : ''"
          @click="selMoney(item.goodsId)"
        >
          <div class="gift-icon">{{ item.goodsName }}</div>
          <div class="gift-text">（{{ item.number }}元）</div>
        </div>
      </div>
      <div class="codeCtn" v-else>
        <div id="qrcode" class="code" ref="twoCode" v-loading="loading"></div>
        <span class="WxText" v-if="isText">请使用微信扫码进行支付</span>
      </div>
      <div class="sign-btns" v-if="isCode">
        <div class="pay" @click="payMoney">立即支付</div>
        <div class="clear" @click="clear">取消</div>
      </div>
    </div>
    <div class="modal" v-if="isRechange == 1">
      <div class="modal-top">
        <div class="modal-title">提现</div>
        <div class="modal-clear" @click="clear">
          <img src="../assets/imgs/shanchu.png" alt />
        </div>
      </div>
      <div class="modalPhone" v-if="isPhone==false">
        <div class="canReflect">可提现</div>
        <div class="iptMoney">
          <input class="getMoney" v-model="speedNum" @change="getMoney" />
          <span class="iptSpeed">加速币</span>
        </div>
        <div class="money" v-if="actualMoney < 400">未达到最低兑换额度</div>
        <div class="money" v-else>预计到账{{ actualMoney }}元</div>
        <div class="promptText">
          <div class="promptIcon">!</div>
          <div class="promptText">
            加速币按照100:1进行兑换，平台需从中收取20%服务费。最低兑换额度50000加速币。
          </div>
        </div>
        <div class="sign-btns">
          <div class="pay" @click="doReflect">立即提现</div>
          <div class="clear" @click="clear">返回</div>
        </div>
      </div>
      <div class="modalPhone" v-else>
        <div class="wxCode">
          <img src="../assets/imgs/ma.jpg" alt="" />
        </div>
        <div class="iptSpeed">请扫码上方二维码,进入微信小程序进行提现操作</div>
      </div>
    </div>
    <div v-if="successPay"></div>
  </div>
</template>

<script>
import QRCode from "qrcodejs2";
export default {
  props: {
    isRechange: {
      type: Number,
    },
    speedNum: {
      type: Number,
    },
  },
  data() {
    return {
      modal: [],
      show: false,
      codeUrl: "",
      id: 0,
      isCode: true,
      isPayCode: false,
      loading: true,
      isText: false,
      actualMoney: 0,
      isPhone: false,
    };
  },
  created() {
    this.initList();
    console.log(this.isCode);
  },
  mounted() {
    this.getRem(1920, 100);
  },
  computed: {
    getInfor() {
      return { ...this.$store.state.infromation };
    },
    successPay() {
      if (this.getInfor.payInformation == "success") {
        this.$message({
          message: "支付成功",
          type: "success",
        });
        this.show = false;
        return true;
      } else {
        return false;
      }
    },
  },
  watch: {
    //监听二维码的url
    codeUrl: {
      handler(newVal, oldVal) {
        if (newVal !== oldVal) {
          this.$nextTick(() => {
            this.Payqrcode({ url: newVal, code: "qrcode" });
          });
        }
      },
    },
    //监听加速币
    speedNum: {
      handler(newVal, oldVal) {
        this.actualMoney = newVal * 0.008;
        console.log(newVal);
      },
    },
  },
  methods: {
    async initList() {
      const result = await this.$post(`/laiwen/goods/frontend/list`, {});
      this.modal = result.list;
      for (let i of this.modal.keys()) {
        let number = this.modal[i].money / 100;
        this.$set(this.modal[i], "number", number);
        this.$set(this.modal[i], "show", false);
      }
    },
    //选择加速币数量
    selMoney(id) {
      this.id = id;
      for (let i of this.modal.keys()) {
        this.modal[i].show = false;
      }
      let res = [...this.modal].find((item) => id == item.goodsId);
      res.show = true;
    },
    clear() {
      this.show = !this.show;
      this.isPhone = false;
      //重置code隐藏和文字隐藏
      this.isCode = true;
      this.isText = false;
    },
    //支付
    payMoney() {
      if (this.id === 0) {
        this.$message.error("请选择充值金额");
        return false;
      }
      this.isCode = false;
      this.$post(
        `/laiwen/order/frontend/unifiedorder1?goodsid=${this.id}`
      ).then((res) => {
        this.codeUrl = res.code_url;
        console.log(res);
      });
    },
    //根据url生成二维码
    Payqrcode({ url, code }) {
      this.loading = false;
      let qrcode = new QRCode(code, {
        width: 132,
        height: 132,
        text: url, // 二维码地址
        colorDark: "#000",
        colorLight: "#fff",
      });
      this.isText = true;
    },
    //输入提现金额
    getMoney() {
      this.actualMoney = this.speedNum * 0.008;
    },
    //提现操作
    doReflect() {
      let user = JSON.parse(localStorage.getItem("user"));
      if (user.phoneNumber) {
        this.isPhone = true;
        // this.$message.warning(`手机用户不能提现`);
        return false;
      }
      if (this.actualMoney < 400) {
        this.$message.warning(`未达到最低兑换额度`);
        return false;
      }
      this.$post(
        `/laiwen/order/order/getmoney?money=${this.actualMoney / 0.008}`
      ).then((res) => {
        console.log(res);
        if (res.msg == "success") {
          this.$message.success(`提现成功,请在微信中查收`);
          this.clear();
          //初始化金额
          this.$parent.initSpeed();
        } else {
          this.$message.warning(`${res.msg}`);
        }
      });
    },
  },
};
</script>

<style scoped>
#sign {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  position: absolute;
  left: 0;
  top: 0;
}
img {
  width: 100%;
  height: 100%;
  display: block;
  flex-shrink: 0;
}
.bg {
  width: 100%;
  height: 100%;
  opacity: 0.25;
  background: black;
  position: absolute;
  /* z-index: 2; */
}
.modal {
  width: 5rem;
  height: 5.52rem;
  display: flex;
  align-items: center;
  position: relative;
  flex-direction: column;
  z-index: 9999;
  margin-top: 1.8rem;
  background: white;
  border-radius: 0.05rem;
}
.modalPhone{
    width: 5rem;
  height: 5.52rem;
  display: flex;
  align-items: center;
  position: relative;
  flex-direction: column;
  z-index: 9999;
  background: white;
  border-radius: 0.05rem;
}
.modal .modal-top {
  width: 100%;
  height: 0.64rem;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding-left: 0.32rem;
  position: relative;
  border-bottom: 0.01rem solid #f5f5f5;
}
.modal .modal-top .modal-title {
  width: 0.7rem;
  height: 0.2rem;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.2rem;
  color: rgba(40, 40, 40, 1);
  opacity: 1;
}
.modal .modal-top .modal-clear {
  width: 0.11rem;
  height: 0.11rem;
  cursor: pointer;
  position: absolute;
  right: 0.32rem;
}
.modal-btns {
  width: 4.35rem;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-top: 0.72rem;
}
.modal-btns::after {
  content: "";
  width: 0.88rem;
}
.modal .modal-single {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  margin-bottom: 0.24rem;
  width: 1.3rem;
  height: 0.96rem;
  background: rgba(255, 255, 255, 1);
  opacity: 1;
  cursor: pointer;
  border-radius: 0.05rem;
  border: 0.01rem solid transparent;
  background: #eeeeee;
  color: #282828;
}
image {
  width: 100%;
  display: block;
  height: 100%;
}
.modal-text {
  width: 0.42rem;
  height: 0.2rem;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.2rem;
  color: rgba(9, 9, 9, 1);
  opacity: 1;
}
.code {
  margin-top: -0.42rem;
}
.codeCtn {
  width: 100%;
  height: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
.WxText {
  font-size: 0.15rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(9, 9, 9, 1);
  margin-top: 0.12rem;
  opacity: 1;
}
.modal-btn {
  width: 0.88rem;
  height: 0.78rem;
  background: rgba(238, 238, 238, 1);
  opacity: 1;
  border-radius: 0.05rem;
  margin-top: 0.08rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
}
.gift-icon {
  font-size: 0.16rem;
}
.modal-btn .gift-icon {
  width: 0.31rem;
  height: 0.31rem;
  margin-top: 0.13rem;
  font-size: 0.16rem;
}
.gift-text {
  margin-top: 0.07rem;
  height: 0.2rem;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.2rem;
  opacity: 1;
}
.sign-btns {
  width: 100%;
  display: flex;
  position: absolute;
  bottom: 0;
  height: 0.84rem;
  align-items: center;
  border-top: 0.01rem solid #f5f5f5;
  justify-content: center;
}
.pay {
  width: 1.22rem;
  height: 0.38rem;
  background: rgba(255, 189, 61, 1);
  opacity: 1;
  border-radius: 0.05rem;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.38rem;
  cursor: pointer;
  text-align: center;
  color: rgba(255, 255, 255, 1);
  opacity: 1;
  outline: none;
  margin-right: 0.2rem;
}
.clear {
  width: 1.22rem;
  height: 0.38rem;
  background: white;
  opacity: 1;
  border-radius: 0.05rem;
  cursor: pointer;
  font-size: 0.14rem;
  font-family: PingFang SC;
  font-weight: 400;
  line-height: 0.38rem;
  border: 0.01rem solid rgba(153, 153, 153, 1);
  text-align: center;
  color: #282828;
  opacity: 1;
}
.clear-btn {
  width: 0.24rem;
  height: 0.24rem;
  border-radius: 50%;
  opacity: 1;
  position: absolute;
  right: -0.24rem;
  top: 0.18rem;
  cursor: pointer;
}
/* 选中后的样式 */
.selected {
  color: #ffbd3d !important;
  border: 0.01rem solid #ffbd3d !important;
  background: white !important;
}
.canReflect {
  font-size: 0.28rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(110, 110, 110, 1);
  opacity: 1;
  margin-top: 0.95rem;
  margin-bottom: 0.32rem;
}
.money {
  font-size: 0.18rem;
  font-family: PingFang SC;
  font-weight: 400;
  margin-bottom: 0.12rem;
}
.iptMoney {
  width: 3.42rem;
  height: 0.52rem;
  background: rgba(244, 244, 244, 1);
  opacity: 1;
  border-radius: 0.3rem;
  position: relative;
  margin-bottom: 0.12rem;
  overflow: hidden;
  display: flex;
  align-items: center;
  box-sizing: border-box;
  padding-left: 0.7rem;
  font-size: 0.24rem;
}
.iptMoney .getMoney {
  width: 2rem;
  height: 0.52rem;
  background: rgba(244, 244, 244, 1);
  opacity: 1;
  border-radius: 0.03rem;
  border: none;
  outline: none;
  color: rgba(110, 110, 110, 1);
  font-size: 0.24rem;
  text-align: center;
}
.promptText {
  width: 3.9rem;
  font-size: 0.18rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(153, 153, 153, 1);
  opacity: 1;
  display: flex;
  margin-left: 0.06rem;
}
.promptText .promptIcon {
  width: 0.2rem;
  height: 0.2rem;
  opacity: 1;
  border: 0.01rem solid rgba(153, 153, 153, 1);
  border-radius: 50%;
  text-align: center;
  line-height: 0.2rem;
  margin-top: 0.02rem;
}
.iptSpeed {
  font-size: 0.18rem;
  font-family: PingFang SC;
  font-weight: 400;
  color: rgba(153, 153, 153, 1);
}
.wxCode {
  width: 2rem;
  height: 2rem;
  margin-top: 1rem;
  margin-bottom: 0.12rem;
}
</style>
